import { createRouter, createWebHistory } from 'vue-router'

// 路由配置
const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/home/Home.vue'),
    meta: {
      title: '首页'
    }
  },
  {
    path: '/products',
    name: 'Products',
    component: () => import('../views/product/ProductList.vue'),
    meta: {
      title: '商品列表'
    }
  },
  {
    path: '/product/:id',
    name: 'ProductDetail',
    component: () => import('../views/product/ProductDetail.vue'),
    meta: {
      title: '商品详情'
    },
    props: true
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import('../views/cart/Cart.vue'),
    meta: {
      title: '购物车'
    }
  },
  {
    path: '/checkout',
    name: 'Checkout',
    component: () => import('../views/order/Checkout.vue'),
    meta: {
      title: '结算'
    }
  },
  {
    path: '/payment/:orderId',
    name: 'Payment',
    component: () => import('../views/order/Payment.vue'),
    meta: {
      title: '支付'
    },
    props: true
  },
  {
    path: '/orders',
    name: 'Orders',
    component: () => import('../views/order/OrderList.vue'),
    meta: {
      title: '我的订单'
    }
  },
  {
    path: '/order/:id',
    name: 'OrderDetail',
    component: () => import('../views/order/OrderDetail.vue'),
    meta: {
      title: '订单详情'
    },
    props: true
  },
  {
    path: '/user',
    name: 'UserCenter',
    component: () => import('../views/user/UserCenter.vue'),
    meta: {
      title: '用户中心'
    },
    children: [
      {
        path: 'profile',
        name: 'UserProfile',
        component: () => import('../views/user/Profile.vue'),
        meta: {
          title: '个人信息'
        }
      },
      {
        path: 'address',
        name: 'UserAddress',
        component: () => import('../views/user/Address.vue'),
        meta: {
          title: '收货地址'
        }
      },
      {
        path: 'refunds',
        name: 'UserRefunds',
        component: () => import('../views/user/Refunds.vue'),
        meta: {
          title: '退货退款'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由拦截器
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title + ' - AI商城'
  }
  next()
})

export default router